<?php

namespace App\Trip\Plane;

use Exception;
use Illuminate\Support\Facades\Cache;
use Maatwebsite\Excel\Facades\Excel;

class PlaneCompany
{

    private array  $seeder;
    private string $carrier_code_column;
    private string $company_name_uy_column;
    private string $company_name_column;
    private string $short_name_column;
    private string $short_name_uy_column;

    public function __construct($refresh = false)
    {

        $this->company_name_column    = "0";
        $this->short_name_column      = "1";
        $this->carrier_code_column    = "2";
        $this->company_name_uy_column = "5";
        $this->short_name_uy_column   = "6";
        $ck                           = "Plane:PlaneCompanies:seeds_with_trans";
        if (Cache::has($ck) && !$refresh) {
            $seeder = Cache::get($ck);
        } else {
            $data   = Excel::toArray(collect(), storage_path('plane/seeds/plane_company_with_trans.xlsx'));
            $seeder = $data[0];
            unset($seeder[0]);
            Cache::forever($ck, $seeder);
        }
        $this->seeder = collect($seeder)->filter(function ($row) {
            return !is_null($row[0]);
        })->toArray();

    }

    /**
     * 全称 维语
     * @param string $carrier_code
     * @return string
     */
    public function getCompanyNameUyByCode(string $carrier_code): string
    {
        return $this->getDataByCode($carrier_code)[$this->company_name_uy_column] ?: trans2uy($this->getCompanyNameByCode($carrier_code));
    }

    private function getDataByCode(string $carrier_code, string $company_name = '')
    {
        if (!$carrier_code) {
            return [];
        }
        $ck = "Plane:PlaneCompanies:{$carrier_code}:{$company_name}:trans";
        if (Cache::has($ck)) {
            return Cache::get($ck);
        } else {
            if ($company_name) {
                $ret = collect($this->seeder)->where($this->carrier_code_column, $carrier_code)
                                             ->firstWhere($this->company_name_column, $company_name);
                if (!$ret) {
                    $ret = collect($this->seeder)->firstWhere($this->carrier_code_column, $carrier_code);
                }
            } else {
                $ret = collect($this->seeder)->firstWhere($this->carrier_code_column, $carrier_code);
            }

            $ret && Cache::forever($ck, $ret);
            return $ret;
        }
    }

    /**
     * 航司全称
     * @param string $carrier_code
     * @return string
     */
    public function getCompanyNameByCode(string $carrier_code): string
    {
        return $this->getDataByCode($carrier_code)[$this->company_name_column] ?? '';
    }

    /**
     * 简称 维语
     * @param string $carrier_code
     * @return string
     * @throws Exception
     */
    public function getCompanyShortNameUyByCode(string $carrier_code): string
    {
        return $this->getDataByCode($carrier_code)[$this->short_name_uy_column] ?: trans2uy($this->getCompanyShortNameByCode($carrier_code));
    }

    /**
     * 航司简称
     * @param string $carrier_code
     * @return string
     */
    public function getCompanyShortNameByCode(string $carrier_code): string
    {
        return $this->getDataByCode($carrier_code)[$this->short_name_column] ?? '';
    }
}